<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<body>
<script type="text/javascript" src="/${initParam.context_root }/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#id").on("change", function(){
		var id = document.getElementById("id").value;
		$.getJSON("/droowa/product/getInvJSON.do",{"id":id}, callback);
	});
});

function callback(data){
	$("#id").parent().next().html(data.name);
}
function chk(){
	if(document.getElementById("id").value=="상품ID"){
		$("#error").html("상품을 선택하세요");
		return false;
	}else if(!document.getElementById("quantity").value.trim()){
		$("#error").html("안전재고수량을 입력하세요");
		return false;
	}
}
function chk2(){
	$("#error").html("");
}
function chk3(){
	$("#error").html("");
}
</script>
<h2 align="center">안전재고등록</h2>
<table align="center"><tr><td height="20" style="color: red;" id="error"></td></tr></table>
<table align="center"  border="1" style="border-collapse: collapse; border-color: #EAEAEA">
	<tr align="center" bgcolor="#BDBDBD">
		<td>상품ID</td>
		<td width="200">상품명</td>
		<td width="40">안전재고</td>
		<td>등록</td>
	</tr>
	<form action="/droowa/product/updateSafeInventory.do" onsubmit="return chk()">
	<tr align="center">
		<td>
			<select name="id" id="id" onchange="chk2()">
				<option value='상품ID'>상품ID</option>
					<c:forEach items="${requestScope.inventory_list}" var="product">
						<option value="${product.id}">
							${product.id}
						</option>
					</c:forEach>
			</select>
		</td>
		<td></td>
		<td>
			<input type="text" name="safeQuantity" id="quantity" onkeyup="chk3()" style="width:40px;">
		</td>
		<td>
			<input type="submit" value="등록">
		</td>
	</tr>
	</form>
</table>
<br>
<table align="center">
	<tr align="center" bgcolor="#BDBDBD">
		<td></td>
		<td width="71">상품ID</td>
		<td width="100">카테고리</td>
		<td width="200">상품명</td>
		<td width="100">제조사</td>
		<td width="40">재고수량</td>
		<td width="40">안전재고수량</td>
	</tr>
	<c:forEach items="${requestScope.inventory_list}" var="inventory">
		<tr align="center">
			<td width="90" height="90">
		<c:choose>
			<c:when test="${inventory.fileName!=null }">
				<img src="/droowa/product-img/${inventory.fileName}" style="width:90px; height=90px;">
			</c:when>
			<c:when test="${inventory.fileName==null}">
				<img src="/droowa/product-img/No_image.jpg" style="width:90px; height=90px;">
			</c:when>	
		</c:choose>
		</td>
			<td>${inventory.id}</td>
			<td>${inventory.categoryName}</td>
			<td>${inventory.name}</td>
			<td>${inventory.brand}</td>
			<td>${inventory.quantity}</td>
			<td>${inventory.safeQuantity}</td>
		</tr>
	</c:forEach>
</table>
<c:if test="${fn:length(requestScope.inventory_list) != 0 }">
<table align="center">
	<tr>
		<td>
<c:choose>
	<c:when test="${requestScope.paging.previousPageGroup}">
		<a href="/droowa/product/inventory.do?page=${requestScope.paging.startPageOfPageGroup-1}">◀</a>
	</c:when>
	<c:otherwise>
		◀
	</c:otherwise>
</c:choose>
<c:forEach begin="${requestScope.paging.startPageOfPageGroup}" end="${requestScope.paging.endPageOfPageGroup}" step="1" var="page">
	<c:choose>
		<c:when test="${page == requestScope.paging.currentPage }">
			<b>${page}</b>
		</c:when>
		<c:otherwise>
			<a href="/${initParam.context_root}/product/inventory.do?page=${page}">${page}</a>
		</c:otherwise>
	</c:choose>
</c:forEach>
<c:choose>
	<c:when test="${requestScope.paging.nextPageGroup}">
		<a href="/${initParam.context_root}/product/inventory.do?page=${requestScope.paging.endPageOfPageGroup+1}">▶</a>
	</c:when>
	<c:otherwise>
		▶
	</c:otherwise>
</c:choose>
		</td>
	</tr>
</table>
</c:if>
</body>
</html>